module MC {
    export module Call {
        export class MsgWin {
            public constructor() {

                this.init();
            }

            private view = new MC.View.MCform('#jiguangWinMCform');
            /** 最新的最大id */
            private maxid = 0;
            /** 当前对象的用户id */
            private uid: any = 0;
            ////目标的id
            private target_id: any = 0;
            private pass: string;

            private init() {
                let mthis = this;

                this.view.vue.msglist = [];
                this.view.vue.loadImg= (eve:any)=>{
                    let img=eve.currentTarget;
                    let src=img.src;
                    let $img=$(img);
                    if(src.indexOf("loadingMobile.gif")>0){
                        $img.addClass("isloadPic");
                    }else{
                        $img.removeClass("isloadPic");
                    }
                  //   console.log("loadImg",img,img.src);
                };
                let callid = MC.get('callid');
                this.target_id = callid;
                /*
                if(isNaN(uid)){
                    this.target_id =uid;
                }else{
                    this.target_id = 'mc_' + uid;
                }*/
                if (callid == MC.user.info.jiguang_id) {
                    MC.msg.alert("无法和自己聊天", () => {
                        MC.back();
                    });
                    return;
                }
                this.uid = MC.user.info.jiguang_id;
                /*
                MC.API.getInfo(this.target_id, (json: any) => {
                    // console.log(json);
                    $('#titleName').text(json.name);
                    $('.callfirst').attr('from_id',this.target_id);
                    $('.callfirst .head').attr('src',json.head);
                });*/


                MC.Call.onSuccess((uid: string) => {

                    this.onMsgReceive();
                });


                $('.btnSend').click(() => {
                    this.setJobSend();
                });

                MC.Call.File($(".upfilepicIco [type=file]"), (json: any) => {
                    console.log(json);
                    // 发送消息
                    JIM.sendSinglePic({
                        'target_username': this.target_id,
                        'image': json
                    }).onSuccess(function (data: any, msg: any) {
                        console.log(data, msg);
                        mthis.addli(msg.content, msg.msg_id);
                        mthis.bottom();
                        //data.code 返回码
                        //data.message 描述
                        //data.msg_id 发送成功后的消息id
                        //data.ctime_ms 消息生成时间,毫秒
                        //data.appkey 用户所属 appkey
                        //data.target_username 用户名
                        //msg.content 发送成功消息体
                    }).onFail(function (data: any) {
                        console.log(data);
                        //同发送单聊文本
                    });
                });


                $('#scrollDiv').on('click', 'img.msgPic', function () {
                    let one = $(this).attr('picurl');
                    let listobj = $('#scrollDiv img.msgPic');
                    let list = [];
                    for (let i = 0; i < listobj.length; i++) {
                        list.push(listobj[i].src);
                    }
                    if (one !== '') {
                        MC.msg.Image(one, list);
                    }
                });

            /*
                $('#scrollDiv').on('click', ' [from_id] .head', function () {
                    let one = $(this).parents('[from_id]');
                    let uid = one.attr('from_id');
                    MC.msg.showZiliao(uid);
                });*/

            }


            /**  监听聊天实时信息 */
            private onMsgReceive() {
                JIM.onMsgReceive((data: any) => {
                    //console.log('onMsgReceive', data);
                    let messages = data.messages;
                    for (let i = 0; i < messages.length; i++) {
                        let one = messages[i];
                        if (one.from_username == this.target_id) {
                            this.addli(one.content, one.msg_id);
                        }
                    }
                    this.bottom();

                    //	setTimeout(() => {
                    this.resetUnreadCount();
                    MC.Call.newmsgAudio();
                    //	}, 2000);
                });

                JIM.onSyncConversation((data: any) => {
                  ///  	console.log("onSyncConversation", data);
                    /*	if (data.length > 0) {
                            $('.callfirst').hide();
                        }*/
                    //	let uid = 'mc_' + MC.get('uid');
                    for (let i = 0; i < data.length; i++) {
                        let one = data[i];
                        if (one.from_username == this.target_id) {
                            //	$('.callfirst').hide();
                            console.log("onSyncConversationone", one);
                            let messages = one.msgs;
                            for (let i = 0; i < messages.length; i++) {
                                let one = messages[i];
                                this.addli(one.content, one.msg_id);
                            }
                        }
                    }
                    setTimeout(() => {
                        this.bottom();
                    }, 800);
                    this.bottom();

                });
                setTimeout(() => {
                    this.resetUnreadCount();
                }, 200);

            }

            ////设置当前聊天为已读了
            private resetUnreadCount() {
                JIM.resetUnreadCount({
                    'username': this.target_id
                });
            }


            private setJobSend() {
                let mthis = this;
                MC.click(() => {

                    let quest = $('#quest').val();
                    if (quest == '') {
                        MC.msg.alert("必须的输入内如");
                        return;
                    }
                    if (quest.length > 140) {
                        MC.msg.alert("输入的内容太多了");
                        return;
                    }
                    $('#quest').val('');

                    // 发送消息
                    JIM.sendSingleMsg({
                        'target_username': this.target_id,
                        'content': quest,
                        extras:{f:'addddsssss123456789'}
                    }).onSuccess(function (data: any, msg: any) {
                        ///console.log(data, msg);
                        mthis.addli(msg.content, msg.msg_id);
                        mthis.bottom();
                       
                    }).onFail(function (data: any) {
                        console.log(data);
                    });


                });
            }

            /**
             * 刷新消息
             */
            private upmsg() {

            }


            private addli(content: any, id: string) {


                let obj = {
                    target_id: content.target_id,
                    from_id: content.from_id,
                    msg_type: content.msg_type,
                    text:'',
                    title:'',
                    desc:'',
                    pic:'',
                    msg_id: id,
                    create_time: MC.API.timeTostr(content.create_time),
                    avaPic: MC.HOST+'/api/app/avatar?jid=' + content.from_id,
                    isload:true,
                    isme:false
                };
                if (content.msg_type == 'text') {
                    obj.text=content.msg_body.text;
                    obj.isload=false;
                }else if(content.msg_type == 'image'){
                    obj.text='/h5/images/loadingMobile.gif';
                    JIM.getResource({
                        'media_id': content.msg_body.media_id,
                    }).onSuccess((data: any)=>{
                         obj.text=data.url;
                         obj.isload=false;
                    });
                }else if(content.msg_type == 'custom'){////其它类型
                    obj.text='';
                    obj.title=content.msg_body.title;
                    obj.desc=content.msg_body.desc;
                    obj.pic=content.msg_body.pic;
                }



                let isme = this.uid == content.from_id;////
                if (isme) {
                    obj.avaPic =MC.HOST+ '/api/app/avatar?jid=' + content.from_id;
                    obj.isme=true;
                }
                this.view.vue.msglist.push(obj);
            }


            /** 滚动到底部 */
            private bottom() {
                $(".speak_window").animate({scrollTop: $(".speak_window").prop("scrollHeight") + 100}, 400);
            }

        }

    }

}